<?php

App::uses('AppController', 'Controller');

/**
 * Tickets Controller
 *
 * @property Ticket $Ticket
 */
class TicketsController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Ticket->recursive = 0;
        $this->paginate = array('order' => 'Ticket.number DESC');
        $this->set('tickets', $this->paginate());
//        $this->increment(2);
    }

    /**
     * index method
     *
     * @return void
     */
    public function getNumber() {

        $this->Ticket->recursive = 0;

        $ticket = $this->Ticket->find('first', array('fields' => array('MAX(Ticket.number )as number')));

        $numberMax = $ticket[0]['number'];

        if (is_null($numberMax)) {
            $numberMax = 0;
        }

        $this->set('number', (++$numberMax));
        $this->layout = 'ajax';
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Ticket->exists($id)) {
            throw new NotFoundException(__('Invalid ticket'));
        }
        $options = array('conditions' => array('Ticket.' . $this->Ticket->primaryKey => $id));
        $this->set('ticket', $this->Ticket->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Ticket->create();
            if ($this->Ticket->save($this->request->data)) {
                $this->Session->setFlash(__('The ticket has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The ticket could not be saved. Please, try again.'));
            }
        }
    }

    /**
     * increment method
     *
     * @return void
     */
    public function increment($number = null) {
//        if ($this->request->is('post')) {
        $this->Ticket->create();
        if ($this->Ticket->saveField('number', $number)) {
            return true;
        } else {
            return false;
        }
//        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->Ticket->exists($id)) {
            throw new NotFoundException(__('Invalid ticket'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Ticket->save($this->request->data)) {
                $this->Session->setFlash(__('The ticket has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The ticket could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Ticket.' . $this->Ticket->primaryKey => $id));
            $this->request->data = $this->Ticket->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Ticket->id = $id;
        if (!$this->Ticket->exists()) {
            throw new NotFoundException(__('Invalid ticket'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Ticket->delete()) {
            $this->Session->setFlash(__('Ticket deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Ticket was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

    /**
     * reset method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function reset() {

        if ($this->Ticket->deleteAll(true)) {
            $this->Session->setFlash(__('Numeros de Tickets reseteados exitosamente'));
            $this->redirect(array('controller' => 'Sales', 'action' => 'index'));
        }
        $this->Session->setFlash(__('Los Numeros de Tickets No han sido reseteados'));
        $this->redirect(array('controller' => 'Sales', 'action' => 'index'));
    }

}
